package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.common.response.sys.SysArticleCategoryResponse;
import com.mars.module.admin.entity.SysArticleCategory;
import com.mars.module.admin.request.SysArticleCategoryRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysArticleCategoryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysArticleCategoryMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysArticleCategory;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysArticleCategoryService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章分类业务层处理
 *
 * @author mars
 * @date 2024-03-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysArticleCategoryServiceImpl implements ISysArticleCategoryService {

    private final SysArticleCategoryMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysArticleCategory add(SysArticleCategoryRequest request) {
        SysArticleCategory entity = SysArticleCategory.builder().build();
        BeanUtils.copyProperties(request, entity);
        if (request.getParentId() == null) {
            entity.setParentId(0L);
        }
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public List<SysArticleCategoryResponse> tree(SysArticleCategoryRequest queryDto) {
        List<SysArticleCategory> list = baseMapper.selectByCondition(queryDto);
        List<SysArticleCategoryResponse> voList = new ArrayList<>();
        for (SysArticleCategory sysMenu : list) {
            SysArticleCategoryResponse sysMenuVo = new SysArticleCategoryResponse();
            if (sysMenu.getParentId().equals(0L)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    @Override
    public List<SysArticleCategoryResponse> getChildren(List<SysArticleCategory> list, Long id) {
        List<SysArticleCategoryResponse> voList = new ArrayList<>();
        for (SysArticleCategory sysMenu : list) {
            SysArticleCategoryResponse sysMenuVo = new SysArticleCategoryResponse();
            if (sysMenu.getParentId() != null && sysMenu.getParentId().equals(id)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysArticleCategoryRequest request) {
        SysArticleCategory entity = SysArticleCategory.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public SysArticleCategory getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysArticleCategory> pageList(SysArticleCategoryRequest request) {
        Page<SysArticleCategory> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysArticleCategory> query = this.buildWrapper(request);
        IPage<SysArticleCategory> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysArticleCategory> list(SysArticleCategoryRequest request) {
        LambdaQueryWrapper<SysArticleCategory> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysArticleCategory> buildWrapper(SysArticleCategoryRequest param) {
        LambdaQueryWrapper<SysArticleCategory> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getCategoryName())){
               query.like(SysArticleCategory::getCategoryName ,param.getCategoryName());
        }
         if (param.getParentId() != null){
               query.like(SysArticleCategory::getParentId ,param.getParentId());
        }
        return query;
    }

}
